package cz.muni.fi.pa036.kiss.daoimpl;

import cz.muni.fi.pa036.kiss.Entity.Kolej;
import cz.muni.fi.pa036.kiss.Entity.MajetekKoleje;
import cz.muni.fi.pa036.kiss.dao.MajetekKolejeDAO;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author peter
 */
public class MajetekKolejeDAOImpl implements MajetekKolejeDAO{

    @PersistenceContext
    private EntityManager em;
    
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }
    
    @Override
    public void create(MajetekKoleje majetek) {
        if (majetek == null) {
            throw new IllegalArgumentException("majetek is null");
        }
        if (majetek.getId() != null) {
            throw new IllegalArgumentException("majetek.id is not null");
        }

        em.persist(majetek);
    }

    @Override
    public MajetekKoleje get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("null id");
        }
        MajetekKoleje majetek = em.find(MajetekKoleje.class, id);
        return majetek;
    }

    @Override
    public void update(MajetekKoleje majetek) {
        if (majetek == null) {
            throw new IllegalArgumentException("majetek is null");
        }
        if (majetek.getId() == null) {
            throw new IllegalArgumentException("majetek.id is null");
        }
        
        em.merge(majetek);
    }

    @Override
    public void remove(MajetekKoleje majetek) {
        if (majetek == null) {
            throw new IllegalArgumentException("bilance is null");
        }
        if (majetek.getId() == null) {
            throw new IllegalArgumentException("bilance.id is null");
        }
        MajetekKoleje c = em.find(MajetekKoleje.class, majetek.getId());
        em.remove(c);
    }

    @Override
    public List<MajetekKoleje> findAll() {
        Query query = em.createQuery("SELECT c FROM MajetekKoleje c");
        List<MajetekKoleje> results = query.getResultList();

        return results;
    }

    @Override
    public List<MajetekKoleje> findByKolej(Kolej kolej) {
        if (kolej == null) {
            throw new IllegalArgumentException("kolej is null");
        }
        if (kolej.getId() == null) {
            throw new IllegalArgumentException("kolej's id is null");
        }

        Query query;
        query = em.createQuery("SELECT o FROM MajetekKoleje o WHERE o.kolej.id = :id");
        query.setParameter("id", kolej.getId());

        List<MajetekKoleje> results = query.getResultList();
        return results;
    }
    
}
